package com.android.settings.ios.general;

import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
//import android.ext.NvRamReserved;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.StatFs;
import android.provider.MediaStore;
import android.telephony.TelephonyManager;

import androidx.core.telephony.TelephonyManagerCompat;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;

import com.android.settings.ios.BaseFragment;
import com.android.settings.ios.OsUtils;
import com.android.settings.ios.R;

import java.util.Locale;

public class GeneralAboutFragment extends BaseFragment {

    private Preference mNamePref;
    private Preference mVersionPref;
    private Preference mModelPref;
    private Preference mCodePref;
    private Preference mSerialPref;

    private Preference mMusicPref;
    private Preference mVideoPref;
    private Preference mPicturePref;
    private Preference mAppsPref;
    private Preference mTotalRomPref;
    private Preference mFreeRomPref;

    private Preference mWifiAddPref;
    private Preference mBluetoothAddPref;
    private Preference mImei1Pref;
    private Preference mImei2Pref;

    private SharedPreferences mPreferences;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.general_about_device);

        mPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());

        mNamePref = findPreference("about_device_name");
        mVersionPref = findPreference("about_device_version");
        mModelPref = findPreference("about_device_model");
        mCodePref = findPreference("about_device_code");
        mSerialPref = findPreference("about_device_serial");

        mMusicPref = findPreference("about_music");
        mVideoPref = findPreference("about_video");
        mPicturePref = findPreference("about_picture");
        mAppsPref = findPreference("about_apps");
        mTotalRomPref = findPreference("about_total_rom");
        mFreeRomPref = findPreference("about_free_rom");

        mWifiAddPref = findPreference("about_wifi_address");
        mBluetoothAddPref = findPreference("about_bluetooth_address");
        mImei1Pref = findPreference("about_imei_1");
        mImei2Pref = findPreference("about_imei_2");
    }

    @Override
    public void onResume() {
        super.onResume();

        //Name
        String name = OsUtils.getPropString("persist.sys.product.name", "iPad");
        mNamePref.setSummary(mPreferences.getString("about_device_name", name));

        //iOS Version
        name = OsUtils.getPropString("persist.sys.fake.softerversion", "17.5.1");
        mVersionPref.setSummary(mPreferences.getString("about_os_version", name));

        //Model Name
        name = OsUtils.getPropString("persist.sys.fake.model.name", "iPad Pro");
        mModelPref.setSummary(mPreferences.getString("about_model_name", name));

        String newSN = null;
        String modelStr = null;
        if ("1".equals(OsUtils.getPropString("ro.ext.serial_bar_code", ""))) {
            String barCode = OsUtils.getPropString("vendor.gsm.serial", "");
            if (barCode != null && barCode.length() > 1) {
                barCode = barCode.substring(0, Math.min(40, barCode.length())).trim();
                String[] strings = barCode.split("#");
                if (strings.length >= 2) {
                    newSN = strings[0];
                    modelStr = strings[1];
                }
            }
        }
        //Model Number
        if (modelStr == null || modelStr.length() == 0) {
            modelStr = OsUtils.getPropString("persist.sys.ext.model", "3F96CH/A");
        }
        mCodePref.setSummary(modelStr);

        //Serial Number
        if (newSN == null || newSN.length() == 0) {
            //newSN = mPreferences.getString("about_serial_code", NvRamReserved.getSN());
        }
        mSerialPref.setSummary(newSN);

//        ContentResolver cr = getActivity().getContentResolver();
//        mMusicPref.setSummary(String.valueOf(countMusics(cr)));
//        mVideoPref.setSummary(String.valueOf(countVideos(cr)));
//        mPicturePref.setSummary(String.valueOf(countPhotos(cr)));
//        mAppsPref.setSummary(String.valueOf(OsUtils.sTotalAppCount));

        StatFs fs = new StatFs("/data");
        float total = fs.getTotalBytes() / 1024.0f / 1024 / 1024;
        float free = fs.getAvailableBytes() / 1024.0f / 1024 / 1024;
        mTotalRomPref.setSummary(String.format(Locale.US, "%.2f GB", total));
        mFreeRomPref.setSummary(String.format(Locale.US, "%.2f GB", free));

//        WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
//        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//        if (wifiInfo != null) {
//            mWifiAddPref.setSummary(wifiInfo.getMacAddress());
//        }
//
//        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
//        if (bluetooth != null) {
//            mBluetoothAddPref.setSummary(bluetooth.getAddress());
//        }

//        TelephonyManager telephonyManager = getActivity().getSystemService(TelephonyManager.class);
//        if (TelephonyManagerCompat.getPhoneCount(telephonyManager) > 1) {
//            mImei1Pref.setSummary(telephonyManager.getDeviceId(0));
//            mImei2Pref.setSummary(telephonyManager.getDeviceId(1));
//        } else {
//            mImei1Pref.setSummary(telephonyManager.getDeviceId());
//            mImei2Pref.setSummary(telephonyManager.getDeviceId());
//        }
    }

    @Override
    protected void onPrepareNewFragmentArgs(Preference preference, Bundle bundle) {
        if (mNamePref == preference) {
            bundle.putString("name", mNamePref.getSummary().toString());
        }
        super.onPrepareNewFragmentArgs(preference, bundle);
    }

    public int countPhotos(ContentResolver cr) {
        int count = 0;
        final String[] columns = {MediaStore.Images.Media._ID};
        Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }

        return count;
    }

    public int countVideos(ContentResolver cr) {
        int count = 0;
        String[] projection = {MediaStore.Video.VideoColumns.DATA};
        Cursor cursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }

        return count;
    }

    public int countMusics(ContentResolver cr) {
        int count = 0;
        String[] projection = {MediaStore.Audio.AudioColumns.DATA};
        Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }

        return count;
    }
}
